<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Prepaid extends CI_Controller {

	public function index()
	{	
		 //prepaid page
		 $d['hello'] ="data";		 
		 $views['_content'] = "prepaid";
		 $this->template->load('template_landing', $views, $d);
	}
	
	public function verifyNumber($num = "")
	{
		$return['status'] = -1;
		
		if($num!="")
		{
			// check if number is valid format, 09171234567
			if((strlen($num)==11) && (substr_compare($num, "09", 0, 2)==0))
			{
				// if true check with API if number is valid
				$this->load->library('Maitaidata');
				$checknum = $this->maitaidata->checkMsisdn($num);
				$checknumresp = $checknum->obj;
				log_message('error', "checknumresp:". $checknumresp);
				// if true will send 0 and now verification code
				if($checknumresp==0)
				{
					$this->load->library('rt');
					
					$vcode = $this->rt->generateVerificationCode();
					
					$msg = "We have received you enrollment request to GoGADGET via Globe website. Your verification code is " . $vcode . ". Please use it to proceed. Thank you!";
					
					if($this->rt->sendsms($num, $msg)) {
						// Save number to cookie for validation
						$this->input->set_cookie(array('name'=>'mobtel', 'value'=>$num, 'expire'=>0, 'path'=>'/'));
			
						// Save to DB na yung cookie + VCODE
						$this->load->model('mobtel');
						if($this->mobtel->saveMobtelToDB($num, $vcode))
						{
							$return['status'] = 0;	
						} else {
							$return['status'] = -1;
						}
					} else {
						$return['status'] = -1;
					}
					
				} else {
					// error2 meron already registered
					$return['status'] = -2;
				}
					
				
			} else {
				// error1
				$return['status'] = -1;
			}
		}
		
		die(json_encode($return));
	}
	
	public function sendVerification(){
	
	}
	
	public function verifyCode($vcode){
		$return['status'] = -1;
		
		if($vcode!="")
		{
			$cookie_mobtel = $this->input->cookie('mobtel');
			$this->load->model('mobtel');
			if($this->mobtel->isInMobtel($cookie_mobtel, $vcode))
			{
				$return['status'] = 0;
				$return['redirect'] = base_url('index.php/userprofile');
			} else {
				$return['status'] = -1;
			}
		}
		
		die(json_encode($return));
	}
	
    public function verifyNumber2($num){
		$return['status'] = 0;
		
		if($num!="")
		{
			$this->load->model('mobtel');
			if($this->mobtel->isInMobtel2($num))
			{
				$return['status'] = -1;
				$return['error'] = "You entered the same number.";
			} else {
				$return['status'] = 0;
			}
		}
		
		die(json_encode($return));
	}
	
	
	public function verifyNumber3($num = "")
	{
		$return['status'] = -1;
		
		if($num!="")
		{
			// check if number is valid format, 09171234567
			if((strlen($num)==11) && (substr_compare($num, "09", 0, 2)==0))
			{
				// if true check with API if number is valid
				$this->load->library('Maitaidata');
				$checknum = $this->maitaidata->checkMsisdn($num);
				$checknumresp = $checknum->obj;
				
				// if true will send 0 and now verification code
				$this->load->model('mobtel');
				if($this->mobtel->isInMobtel2($num))
				{
					// error2 meron already registered
					$return['status'] = -2;
				} else {
				
					if($checknumresp==0)
				    {
				
					  $this->load->library('rt');
					
					  $vcode = $this->rt->generateVerificationCode();
					
					  $msg = "We have received you enrollment request to GoGADGET via Globe website. Your verification code is " . $vcode . ". Please use it to proceed. Thank you!";
					
					  if($this->rt->sendsms($num, $msg)) {
						// Save number to cookie for validation
						//$this->input->set_cookie(array('name'=>'mobtel', 'value'=>$num, 'expire'=>0, 'path'=>'/'));
			
						// Save to DB na yung cookie + VCODE
						$mobtel = $this->input->cookie('mobtel');
						$this->load->model('mobtel');
						if($this->mobtel->saveCodeToDB($mobtel, $vcode))
						{
							$return['status'] = 0;	
						} else {
							$return['status'] = -1;
						}
					  } else {
						$return['status'] = -1;
					  }
					}
					else{
						$return['status'] = -1;
					}
				}
					
				
			} else {
				// error1
				$return['status'] = -1;
			}
		}
		
		die(json_encode($return));
	}
	
	public function saveNumber(){
		
			$this->load->model('mobtel');
			if($this->mobtel->saveNumber())
			{
				$return['status'] = 0;
				$return['error'] = "You're number was successfully updated.";
			} else {
				$return['status'] = -1;
				$return['error'] = "You're number was not successfully updated.";
			}
		
		die(json_encode($return));
	}
	
	public function saveNonGlobe() {
		$this->load->model('mobtel');
		if($this->mobtel->saveNonGlobe())
		{
			$return['status'] = 0;
			$return['error'] = "You're number was successfully updated.";
		} else {
			$return['status'] = -1;
			$return['error'] = "You're number was not successfully updated.";
		}
		die(json_encode($return));
	}

	public function verifyNumberNonglobe($num = "")
	{
		$return['status'] = -1;
		
		if($num!="")
		{
			// check if number is valid format, 09171234567
			if((strlen($num)==11) && (substr_compare($num, "09", 0, 2)==0))
			{
				
				// if true will send 0 and now verification code
				$this->load->model('mobtel');
				if($this->mobtel->isInMobtel2($num))
				{
					// error2 meron already registered
					$return['status'] = -2;
				} else {
				
				
					  $this->load->library('rt');
					
					  $vcode = $this->rt->generateVerificationCode();
					
					  $msg = "We have received you enrollment request to GoGADGET via Globe website. Your verification code is " . $vcode . ". Please use it to proceed. Thank you!";
					
					  if($this->rt->sendsms($num, $msg)) {
						// Save number to cookie for validation
						//$this->input->set_cookie(array('name'=>'mobtel', 'value'=>$num, 'expire'=>0, 'path'=>'/'));
			
						// Save to DB na yung cookie + VCODE
						$mobtel = $this->input->cookie('mobtel');
						$this->load->model('mobtel');
						if($this->mobtel->saveCodeToDB($mobtel, $vcode))
						{
							$return['status'] = 0;	
						} else {
							$return['status'] = -1;
						}
					  } else {
						$return['status'] = -1;
					  }
				}
					
				
			} else {
				// error1
				$return['status'] = -1;
			}
		}
		
		die(json_encode($return));
	}
	
	
}
